import Vue from 'vue'
import VueRouter from 'vue-router'
// 布局组件
// import BasicLayout from '@/layout/BasicLayout'
import UserLayout from '@/layout/UserLayout'
import MainLayout from '@/layout/MainLayout'
// 应用组件
import Login from '@/views/user/Login'
import Register from '@/views/user/Register'
import Globe from '@/views/gis/Globe'
import NotFound from '@/views/404'
import Research from '@/views/project/Research'
import Software from '@/views/project/Software'
import WorkLog from '@/views/work/WorkLog'
import TimeSheet from '@/views/work/TimeSheet'
import Performance from '@/views/work/Performance'
import Weekly from '@/views/work/Weekly'
import Dashboard from '@/views/dashboard/Dashboard'
import Users from '@/views/dashboard/Users'
import Rights from '@/views/sys/Rights'
import Roles from '@/views/sys/Roles'
Vue.use(VueRouter)

// 重写VueRouter的push，解决NavigationDuplicated的问题
// const originalPush = VueRouter.prototype.push
// VueRouter.prototype.push = function push(location) {
//   return originalPush.call(this, location).catch((err) => err)
// }

/**
 * name: 有name属性，菜单才加载,
 * hidden: 不在菜单栏中显示
 * meta: {
 *  roles: ['admin', 'customer'],
 *  title: 'title',
 *  icon: 'svg-name',
 * }
 */
const routes = [
  // 用户登录、注册页面，不在主页菜单中
  {
    path: '/',
    component: UserLayout,
    redirect: '/login',
    children: [
      {
        path: 'login',
        name: 'login',
        meta: { title: '登录' },
        component: Login
      },
      {
        path: 'register',
        name: 'register',
        meta: { title: '注册' },
        component: Register
      }
    ]
  },
  // Dashboard
  {
    path: '/dashboard',
    component: MainLayout,
    redirect: '/home',
    children: [
      {
        path: '/home',
        name: 'Home',
        component: Dashboard,
        meta: { title: 'Home' }
      },
      {
        path: '/users',
        name: 'Users',
        component: Users,
        meta: { title: '用户列表' }
      },
      {
        path: '/rights',
        name: 'Rights',
        component: Rights,
        meta: { title: '权限列表' }
      },
      {
        path: '/roles',
        name: 'Roles',
        component: Roles,
        meta: { title: '角色列表' }
      }
    ]
  },
  // GIS 主页
  {
    path: '/gis',
    component: MainLayout,
    name: 'gis',
    meta: { title: 'GIS Home' },
    children: [
      {
        path: '/gis/globe',
        name: 'globe',
        meta: { title: '三维场景', icon: '' },
        component: Globe
      }
    ]
  },
  // 科研、软件项目管理
  {
    path: '/project',
    component: MainLayout,
    name: 'project',
    meta: { title: '项目管理', icon: 'dashboard' },
    children: [
      {
        path: '/research',
        name: 'research',
        meta: { title: '科研项目', icon: '' },
        component: Research
      },
      {
        path: '/software',
        name: 'software',
        meta: { title: '软件项目', icon: '' },
        component: Software
      }
    ]
  },
  // 个人考勤
  {
    path: '/work',
    component: MainLayout,
    name: 'work',
    meta: { title: '工作考勤', icon: '' },
    children: [
      {
        path: '/work/worklog',
        name: 'worklog',
        meta: { title: '工作日志', icon: '' },
        component: WorkLog
      },
      {
        path: '/work/timesheet',
        name: 'timesheet',
        meta: { title: '考勤统计', icon: '' },
        component: TimeSheet
      },
      {
        path: '/work/performance',
        name: 'performance',
        meta: { title: '效能考核', icon: '' },
        component: Performance
      },
      {
        path: '/work/weekly',
        name: 'weekly',
        meta: { title: '工作周报', icon: '' },
        component: Weekly
      }
    ]
  },
  // 404
  { path: '*', name: '404', component: NotFound }
]

const router = new VueRouter({
  mode: 'history',
  routes
})

// 路由导航, next势函数，表示放行和跳转
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  // token存在就放行
  next()
})

export default router
